
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.admin.options &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-admin-options">
            
  <h1>Source code for django.contrib.admin.options</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">partial</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="n">update_wrapper</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="k">import</span> <span class="n">quote</span> <span class="k">as</span> <span class="n">urlquote</span>

<span class="kn">from</span> <span class="nn">django</span> <span class="k">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">messages</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin</span> <span class="k">import</span> <span class="n">helpers</span><span class="p">,</span> <span class="n">widgets</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.checks</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">BaseModelAdminChecks</span><span class="p">,</span> <span class="n">InlineModelAdminChecks</span><span class="p">,</span> <span class="n">ModelAdminChecks</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.exceptions</span> <span class="k">import</span> <span class="n">DisallowedModelAdminToField</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.templatetags.admin_urls</span> <span class="k">import</span> <span class="n">add_preserved_filters</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.utils</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">NestedObjects</span><span class="p">,</span> <span class="n">construct_change_message</span><span class="p">,</span> <span class="n">flatten_fieldsets</span><span class="p">,</span>
    <span class="n">get_deleted_objects</span><span class="p">,</span> <span class="n">lookup_needs_distinct</span><span class="p">,</span> <span class="n">model_format_dict</span><span class="p">,</span>
    <span class="n">model_ngettext</span><span class="p">,</span> <span class="n">quote</span><span class="p">,</span> <span class="n">unquote</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.views.autocomplete</span> <span class="k">import</span> <span class="n">AutocompleteJsonView</span>
<span class="kn">from</span> <span class="nn">django.contrib.admin.widgets</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">AutocompleteSelect</span><span class="p">,</span> <span class="n">AutocompleteSelectMultiple</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="k">import</span> <span class="n">get_permission_codename</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">FieldDoesNotExist</span><span class="p">,</span> <span class="n">FieldError</span><span class="p">,</span> <span class="n">PermissionDenied</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.core.paginator</span> <span class="k">import</span> <span class="n">Paginator</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span><span class="p">,</span> <span class="n">router</span><span class="p">,</span> <span class="n">transaction</span>
<span class="kn">from</span> <span class="nn">django.db.models.constants</span> <span class="k">import</span> <span class="n">LOOKUP_SEP</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields</span> <span class="k">import</span> <span class="n">BLANK_CHOICE_DASH</span>
<span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="k">import</span> <span class="n">DELETION_FIELD_NAME</span><span class="p">,</span> <span class="n">all_valid</span>
<span class="kn">from</span> <span class="nn">django.forms.models</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">BaseInlineFormSet</span><span class="p">,</span> <span class="n">inlineformset_factory</span><span class="p">,</span> <span class="n">modelform_defines_fields</span><span class="p">,</span>
    <span class="n">modelform_factory</span><span class="p">,</span> <span class="n">modelformset_factory</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.forms.widgets</span> <span class="k">import</span> <span class="n">CheckboxSelectMultiple</span><span class="p">,</span> <span class="n">SelectMultiple</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.http.response</span> <span class="k">import</span> <span class="n">HttpResponseBase</span>
<span class="kn">from</span> <span class="nn">django.template.response</span> <span class="k">import</span> <span class="n">SimpleTemplateResponse</span><span class="p">,</span> <span class="n">TemplateResponse</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="k">import</span> <span class="n">method_decorator</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="k">import</span> <span class="n">urlencode</span>
<span class="kn">from</span> <span class="nn">django.utils.inspect</span> <span class="k">import</span> <span class="n">get_func_args</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="k">import</span> <span class="n">capfirst</span><span class="p">,</span> <span class="n">format_lazy</span><span class="p">,</span> <span class="n">get_text_list</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">gettext</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">ngettext</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.csrf</span> <span class="k">import</span> <span class="n">csrf_protect</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="k">import</span> <span class="n">RedirectView</span>

<span class="n">IS_POPUP_VAR</span> <span class="o">=</span> <span class="s1">&#39;_popup&#39;</span>
<span class="n">TO_FIELD_VAR</span> <span class="o">=</span> <span class="s1">&#39;_to_field&#39;</span>


<span class="n">HORIZONTAL</span><span class="p">,</span> <span class="n">VERTICAL</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>


<span class="k">def</span> <span class="nf">get_content_type_for_model</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="c1"># Since this module gets imported in the application&#39;s root package,</span>
    <span class="c1"># it cannot import models from other applications at the module level.</span>
    <span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="k">import</span> <span class="n">ContentType</span>
    <span class="k">return</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">for_concrete_model</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_ul_class</span><span class="p">(</span><span class="n">radio_style</span><span class="p">):</span>
    <span class="k">return</span> <span class="s1">&#39;radiolist&#39;</span> <span class="k">if</span> <span class="n">radio_style</span> <span class="o">==</span> <span class="n">VERTICAL</span> <span class="k">else</span> <span class="s1">&#39;radiolist inline&#39;</span>


<span class="k">class</span> <span class="nc">IncorrectLookupParameters</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>


<span class="c1"># Defaults for formfield_overrides. ModelAdmin subclasses can change this</span>
<span class="c1"># by adding to ModelAdmin.formfield_overrides.</span>

<span class="n">FORMFIELD_FOR_DBFIELD_DEFAULTS</span> <span class="o">=</span> <span class="p">{</span>
    <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">:</span> <span class="p">{</span>
        <span class="s1">&#39;form_class&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">SplitDateTimeField</span><span class="p">,</span>
        <span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminSplitDateTime</span>
    <span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminDateWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">TimeField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminTimeWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminTextareaWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">URLField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminURLFieldWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminIntegerFieldWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">BigIntegerField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminBigIntegerFieldWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminTextInputWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">ImageField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminFileWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminFileWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">EmailField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminEmailInputWidget</span><span class="p">},</span>
    <span class="n">models</span><span class="o">.</span><span class="n">UUIDField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminUUIDInputWidget</span><span class="p">},</span>
<span class="p">}</span>

<span class="n">csrf_protect_m</span> <span class="o">=</span> <span class="n">method_decorator</span><span class="p">(</span><span class="n">csrf_protect</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">BaseModelAdmin</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">MediaDefiningClass</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Functionality common to both ModelAdmin and InlineAdmin.&quot;&quot;&quot;</span>

    <span class="n">autocomplete_fields</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">raw_id_fields</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">fieldsets</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span>
    <span class="n">filter_vertical</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">filter_horizontal</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">radio_fields</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">prepopulated_fields</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">formfield_overrides</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">readonly_fields</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">ordering</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">sortable_by</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">view_on_site</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">show_full_result_count</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">checks_class</span> <span class="o">=</span> <span class="n">BaseModelAdminChecks</span>

    <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">checks_class</span><span class="p">()</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Merge FORMFIELD_FOR_DBFIELD_DEFAULTS with the formfield_overrides</span>
        <span class="c1"># rather than simply overwriting.</span>
        <span class="n">overrides</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">FORMFIELD_FOR_DBFIELD_DEFAULTS</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">overrides</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span> <span class="o">=</span> <span class="n">overrides</span>

    <span class="k">def</span> <span class="nf">formfield_for_dbfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying the form Field instance for a given database Field</span>
<span class="sd">        instance.</span>

<span class="sd">        If kwargs are given, they&#39;re passed to the form Field&#39;s constructor.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If the field specifies choices, we don&#39;t need to look for special</span>
        <span class="c1"># admin widgets - we just need to use a select widget of some kind.</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">choices</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_choice_field</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># ForeignKey or ManyToManyFields</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">)):</span>
            <span class="c1"># Combine the field kwargs with any options for formfield_overrides.</span>
            <span class="c1"># Make sure the passed in **kwargs override anything in</span>
            <span class="c1"># formfield_overrides because **kwargs is more specific, and should</span>
            <span class="c1"># always win.</span>
            <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="vm">__class__</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span><span class="p">:</span>
                <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span><span class="p">[</span><span class="n">db_field</span><span class="o">.</span><span class="vm">__class__</span><span class="p">],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span>

            <span class="c1"># Get the correct formfield.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">):</span>
                <span class="n">formfield</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_foreignkey</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">):</span>
                <span class="n">formfield</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

            <span class="c1"># For non-raw_id fields, wrap the widget with a wrapper that adds</span>
            <span class="c1"># extra HTML -- the &quot;add other&quot; interface -- to the end of the</span>
            <span class="c1"># rendered output. formfield can be None if it came from a</span>
            <span class="c1"># OneToOneField with parent_link=True or a M2M intermediary.</span>
            <span class="k">if</span> <span class="n">formfield</span> <span class="ow">and</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw_id_fields</span><span class="p">:</span>
                <span class="n">related_modeladmin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                <span class="n">wrapper_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">if</span> <span class="n">related_modeladmin</span><span class="p">:</span>
                    <span class="n">wrapper_kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
                        <span class="n">can_add_related</span><span class="o">=</span><span class="n">related_modeladmin</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
                        <span class="n">can_change_related</span><span class="o">=</span><span class="n">related_modeladmin</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
                        <span class="n">can_delete_related</span><span class="o">=</span><span class="n">related_modeladmin</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
                        <span class="n">can_view_related</span><span class="o">=</span><span class="n">related_modeladmin</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
                    <span class="p">)</span>
                <span class="n">formfield</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">RelatedFieldWidgetWrapper</span><span class="p">(</span>
                    <span class="n">formfield</span><span class="o">.</span><span class="n">widget</span><span class="p">,</span> <span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">,</span> <span class="o">**</span><span class="n">wrapper_kwargs</span>
                <span class="p">)</span>

            <span class="k">return</span> <span class="n">formfield</span>

        <span class="c1"># If we&#39;ve got overrides for the formfield defined, use &#39;em. **kwargs</span>
        <span class="c1"># passed to formfield_for_dbfield override the defaults.</span>
        <span class="k">for</span> <span class="n">klass</span> <span class="ow">in</span> <span class="n">db_field</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">mro</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">klass</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span><span class="p">:</span>
                <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="o">**</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_overrides</span><span class="p">[</span><span class="n">klass</span><span class="p">]),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">}</span>
                <span class="k">return</span> <span class="n">db_field</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c1"># For any other type of field, just call its formfield() method.</span>
        <span class="k">return</span> <span class="n">db_field</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_choice_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a form Field for a database Field that has declared choices.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If the field is named as a radio_field, use a RadioSelect</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">radio_fields</span><span class="p">:</span>
            <span class="c1"># Avoid stomping on custom widget/choices arguments.</span>
            <span class="k">if</span> <span class="s1">&#39;widget&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminRadioSelect</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="p">{</span>
                    <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="n">get_ul_class</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">radio_fields</span><span class="p">[</span><span class="n">db_field</span><span class="o">.</span><span class="n">name</span><span class="p">]),</span>
                <span class="p">})</span>
            <span class="k">if</span> <span class="s1">&#39;choices&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;choices&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">db_field</span><span class="o">.</span><span class="n">get_choices</span><span class="p">(</span>
                    <span class="n">include_blank</span><span class="o">=</span><span class="n">db_field</span><span class="o">.</span><span class="n">blank</span><span class="p">,</span>
                    <span class="n">blank_choice</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;None&#39;</span><span class="p">))]</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">db_field</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_field_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        If the ModelAdmin specifies ordering, the queryset should respect that</span>
<span class="sd">        ordering.  Otherwise don&#39;t specify the queryset, let the field decide</span>
<span class="sd">        (return None in that case).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">related_admin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">related_admin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">ordering</span> <span class="o">=</span> <span class="n">related_admin</span><span class="o">.</span><span class="n">get_ordering</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ordering</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">ordering</span> <span class="o">!=</span> <span class="p">():</span>
                <span class="k">return</span> <span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="o">*</span><span class="n">ordering</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">formfield_for_foreignkey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a form Field for a ForeignKey.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">db</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;using&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="s1">&#39;widget&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_autocomplete_fields</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">AutocompleteSelect</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw_id_fields</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">ForeignKeyRawIdWidget</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">radio_fields</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">AdminRadioSelect</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="p">{</span>
                    <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="n">get_ul_class</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">radio_fields</span><span class="p">[</span><span class="n">db_field</span><span class="o">.</span><span class="n">name</span><span class="p">]),</span>
                <span class="p">})</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;empty_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;None&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">blank</span> <span class="k">else</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="s1">&#39;queryset&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_field_queryset</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;queryset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">queryset</span>

        <span class="k">return</span> <span class="n">db_field</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a form Field for a ManyToManyField.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If it uses an intermediary model that isn&#39;t auto created, don&#39;t show</span>
        <span class="c1"># a field in admin.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="n">db</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;using&#39;</span><span class="p">)</span>

        <span class="n">autocomplete_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_autocomplete_fields</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">autocomplete_fields</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">AutocompleteSelectMultiple</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw_id_fields</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">ManyToManyRawIdWidget</span><span class="p">(</span><span class="n">db_field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">filter_vertical</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">filter_horizontal</span><span class="p">]:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">widgets</span><span class="o">.</span><span class="n">FilteredSelectMultiple</span><span class="p">(</span>
                <span class="n">db_field</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
                <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_vertical</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="s1">&#39;queryset&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_field_queryset</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;queryset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">queryset</span>

        <span class="n">form_field</span> <span class="o">=</span> <span class="n">db_field</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">form_field</span><span class="o">.</span><span class="n">widget</span><span class="p">,</span> <span class="n">SelectMultiple</span><span class="p">)</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">form_field</span><span class="o">.</span><span class="n">widget</span><span class="p">,</span> <span class="p">(</span><span class="n">CheckboxSelectMultiple</span><span class="p">,</span> <span class="n">AutocompleteSelectMultiple</span><span class="p">))):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Hold down &quot;Control&quot;, or &quot;Command&quot; on a Mac, to select more than one.&#39;</span><span class="p">)</span>
            <span class="n">help_text</span> <span class="o">=</span> <span class="n">form_field</span><span class="o">.</span><span class="n">help_text</span>
            <span class="n">form_field</span><span class="o">.</span><span class="n">help_text</span> <span class="o">=</span> <span class="n">format_lazy</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> </span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">help_text</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span> <span class="k">if</span> <span class="n">help_text</span> <span class="k">else</span> <span class="n">msg</span>
        <span class="k">return</span> <span class="n">form_field</span>

    <span class="k">def</span> <span class="nf">get_autocomplete_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of ForeignKey and/or ManyToMany fields which should use</span>
<span class="sd">        an autocomplete widget.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">autocomplete_fields</span>

    <span class="k">def</span> <span class="nf">get_view_on_site_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">view_on_site</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">view_on_site</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">view_on_site</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">view_on_site</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;get_absolute_url&#39;</span><span class="p">):</span>
            <span class="c1"># use the ContentType lookup if view_on_site is True</span>
            <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:view_on_site&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span>
                <span class="s1">&#39;content_type_id&#39;</span><span class="p">:</span> <span class="n">get_content_type_for_model</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
                <span class="s1">&#39;object_id&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span>
            <span class="p">})</span>

    <span class="k">def</span> <span class="nf">get_empty_value_display</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the empty_value_display set on ModelAdmin or AdminSite.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">empty_value_display</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">empty_value_display</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_exclude</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying exclude.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">exclude</span>

    <span class="k">def</span> <span class="nf">get_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying fields.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span>
        <span class="c1"># _get_form_for_get_fields() is implemented in subclasses.</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_form_for_get_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="o">*</span><span class="n">form</span><span class="o">.</span><span class="n">base_fields</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">get_readonly_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">get_fieldsets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying fieldsets.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fieldsets</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">fieldsets</span>
        <span class="k">return</span> <span class="p">[(</span><span class="kc">None</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)})]</span>

    <span class="k">def</span> <span class="nf">get_ordering</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying field ordering.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordering</span> <span class="ow">or</span> <span class="p">()</span>  <span class="c1"># otherwise we might try to *None, which is bad ;)</span>

    <span class="k">def</span> <span class="nf">get_readonly_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying custom readonly fields.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">readonly_fields</span>

    <span class="k">def</span> <span class="nf">get_prepopulated_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for specifying custom prepopulated fields.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">prepopulated_fields</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a QuerySet of all model instances that can be edited by the</span>
<span class="sd">        admin site. This is used by changelist_view.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
        <span class="c1"># TODO: this should be handled by some parameter to the ChangeList.</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_ordering</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ordering</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="o">*</span><span class="n">ordering</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">qs</span>

    <span class="k">def</span> <span class="nf">get_sortable_by</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Hook for specifying which fields can be sorted in the changelist.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sortable_by</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">sortable_by</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_list_display</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lookup_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.filters</span> <span class="k">import</span> <span class="n">SimpleListFilter</span>

        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="c1"># Check FKey lookups that are allowed, so that popups produced by</span>
        <span class="c1"># ForeignKeyRawIdWidget, on the basis of ForeignKey.limit_choices_to,</span>
        <span class="c1"># are allowed to work.</span>
        <span class="k">for</span> <span class="n">fk_lookup</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_fkey_lookups</span><span class="p">:</span>
            <span class="c1"># As ``limit_choices_to`` can be a callable, invoke it here.</span>
            <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">fk_lookup</span><span class="p">):</span>
                <span class="n">fk_lookup</span> <span class="o">=</span> <span class="n">fk_lookup</span><span class="p">()</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">lookup</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="ow">in</span> <span class="n">widgets</span><span class="o">.</span><span class="n">url_params_from_lookup_dict</span><span class="p">(</span><span class="n">fk_lookup</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">return</span> <span class="kc">True</span>

        <span class="n">relation_parts</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">prev_field</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">lookup</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">field</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
                <span class="c1"># Lookups on nonexistent fields are ok, since they&#39;re ignored</span>
                <span class="c1"># later.</span>
                <span class="k">break</span>
            <span class="c1"># It is allowed to filter on values that would be found from local</span>
            <span class="c1"># model anyways. For example, if you filter on employee__department__id,</span>
            <span class="c1"># then the id value would be found already from employee__department_id.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">prev_field</span> <span class="ow">or</span> <span class="p">(</span><span class="n">prev_field</span><span class="o">.</span><span class="n">is_relation</span> <span class="ow">and</span>
                                  <span class="n">field</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">prev_field</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">target_fields</span><span class="p">):</span>
                <span class="n">relation_parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;get_path_info&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
                <span class="c1"># This is not a relational field, so further parts</span>
                <span class="c1"># must be transforms.</span>
                <span class="k">break</span>
            <span class="n">prev_field</span> <span class="o">=</span> <span class="n">field</span>
            <span class="n">model</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">to_opts</span><span class="o">.</span><span class="n">model</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">relation_parts</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># Either a local field filter, or no fields at all.</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">valid_lookups</span> <span class="o">=</span> <span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">date_hierarchy</span><span class="p">}</span>
        <span class="k">for</span> <span class="n">filter_item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_filter</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filter_item</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">filter_item</span><span class="p">,</span> <span class="n">SimpleListFilter</span><span class="p">):</span>
                <span class="n">valid_lookups</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">filter_item</span><span class="o">.</span><span class="n">parameter_name</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filter_item</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                <span class="n">valid_lookups</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">filter_item</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">valid_lookups</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">filter_item</span><span class="p">)</span>

        <span class="c1"># Is it a valid relational lookup?</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="p">{</span>
            <span class="n">LOOKUP_SEP</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">relation_parts</span><span class="p">),</span>
            <span class="n">LOOKUP_SEP</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">relation_parts</span> <span class="o">+</span> <span class="p">[</span><span class="n">part</span><span class="p">])</span>
        <span class="p">}</span><span class="o">.</span><span class="n">isdisjoint</span><span class="p">(</span><span class="n">valid_lookups</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">to_field_allowed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">to_field</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the model associated with this admin should be</span>
<span class="sd">        allowed to be referenced by the specified field.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">to_field</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="c1"># Always allow referencing the primary key since it&#39;s already possible</span>
        <span class="c1"># to get this information from the change view URL.</span>
        <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Allow reverse relationships to models defining m2m fields if they</span>
        <span class="c1"># target the specified field.</span>
        <span class="k">for</span> <span class="n">many_to_many</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">many_to_many</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">many_to_many</span><span class="o">.</span><span class="n">m2m_target_field_name</span><span class="p">()</span> <span class="o">==</span> <span class="n">to_field</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Make sure at least one of the models registered for this site</span>
        <span class="c1"># references this field through a FK or a M2M relationship.</span>
        <span class="n">registered_models</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">model</span><span class="p">,</span> <span class="n">admin</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">_registry</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">registered_models</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="n">admin</span><span class="o">.</span><span class="n">inlines</span><span class="p">:</span>
                <span class="n">registered_models</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">inline</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>

        <span class="n">related_objects</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_fields</span><span class="p">(</span><span class="n">include_hidden</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">auto_created</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">concrete</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">related_object</span> <span class="ow">in</span> <span class="n">related_objects</span><span class="p">:</span>
            <span class="n">related_model</span> <span class="o">=</span> <span class="n">related_object</span><span class="o">.</span><span class="n">related_model</span>
            <span class="n">remote_field</span> <span class="o">=</span> <span class="n">related_object</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span>
            <span class="k">if</span> <span class="p">(</span><span class="nb">any</span><span class="p">(</span><span class="nb">issubclass</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">related_model</span><span class="p">)</span> <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">registered_models</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="nb">hasattr</span><span class="p">(</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;get_related_field&#39;</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="n">remote_field</span><span class="o">.</span><span class="n">get_related_field</span><span class="p">()</span> <span class="o">==</span> <span class="n">field</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">True</span>

        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">has_add_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the given request has permission to add an object.</span>
<span class="sd">        Can be overridden by the user in subclasses.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="n">codename</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;add&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">has_change_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the given request has permission to change the given</span>
<span class="sd">        Django model instance, the default implementation doesn&#39;t examine the</span>
<span class="sd">        `obj` parameter.</span>

<span class="sd">        Can be overridden by the user in subclasses. In such case it should</span>
<span class="sd">        return True if the given request has permission to change the `obj`</span>
<span class="sd">        model instance. If `obj` is None, this should return True if the given</span>
<span class="sd">        request has permission to change *any* object of the given type.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="n">codename</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;change&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">has_delete_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the given request has permission to change the given</span>
<span class="sd">        Django model instance, the default implementation doesn&#39;t examine the</span>
<span class="sd">        `obj` parameter.</span>

<span class="sd">        Can be overridden by the user in subclasses. In such case it should</span>
<span class="sd">        return True if the given request has permission to delete the `obj`</span>
<span class="sd">        model instance. If `obj` is None, this should return True if the given</span>
<span class="sd">        request has permission to delete *any* object of the given type.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="n">codename</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;delete&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">has_view_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the given request has permission to view the given</span>
<span class="sd">        Django model instance. The default implementation doesn&#39;t examine the</span>
<span class="sd">        `obj` parameter.</span>

<span class="sd">        If overridden by the user in subclasses, it should return True if the</span>
<span class="sd">        given request has permission to view the `obj` model instance. If `obj`</span>
<span class="sd">        is None, it should return True if the request has permission to view</span>
<span class="sd">        any object of the given type.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="n">codename_view</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="n">codename_change</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;change&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename_view</span><span class="p">))</span> <span class="ow">or</span>
            <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename_change</span><span class="p">))</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_view_or_change_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_module_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the given request has any permission in the given</span>
<span class="sd">        app label.</span>

<span class="sd">        Can be overridden by the user in subclasses. In such case it should</span>
<span class="sd">        return True if the given request has permission to view the module on</span>
<span class="sd">        the admin index page and access the module&#39;s index page. Overriding it</span>
<span class="sd">        does not restrict access to the add, change or delete views. Use</span>
<span class="sd">        `ModelAdmin.has_(add|change|delete)_permission` for that.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_module_perms</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">)</span>


<div class="viewcode-block" id="ModelAdmin"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin">[docs]</a><span class="k">class</span> <span class="nc">ModelAdmin</span><span class="p">(</span><span class="n">BaseModelAdmin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Encapsulate all admin options and functionality for a given model.&quot;&quot;&quot;</span>

    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__str__&#39;</span><span class="p">,)</span>
    <span class="n">list_display_links</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">list_select_related</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">list_per_page</span> <span class="o">=</span> <span class="mi">100</span>
    <span class="n">list_max_show_all</span> <span class="o">=</span> <span class="mi">200</span>
    <span class="n">list_editable</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">search_fields</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">date_hierarchy</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">save_as</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">save_as_continue</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">save_on_top</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">paginator</span> <span class="o">=</span> <span class="n">Paginator</span>
    <span class="n">preserve_filters</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="c1"># Custom templates (designed to be over-ridden in subclasses)</span>
    <span class="n">add_form_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">change_form_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">change_list_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">delete_confirmation_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">delete_selected_confirmation_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">object_history_template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">popup_response_template</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Actions</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">action_form</span> <span class="o">=</span> <span class="n">helpers</span><span class="o">.</span><span class="n">ActionForm</span>
    <span class="n">actions_on_top</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">actions_on_bottom</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">actions_selection_counter</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">checks_class</span> <span class="o">=</span> <span class="n">ModelAdminChecks</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">admin_site</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span> <span class="o">=</span> <span class="n">admin_site</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.get_inline_instances"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_inline_instances">[docs]</a>    <span class="k">def</span> <span class="nf">get_inline_instances</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">inline_instances</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">inline_class</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">inlines</span><span class="p">:</span>
            <span class="n">inline</span> <span class="o">=</span> <span class="n">inline_class</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">request</span><span class="p">:</span>
                <span class="n">inline_has_add_permission</span> <span class="o">=</span> <span class="n">inline</span><span class="o">.</span><span class="n">_has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">inline</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="ow">or</span>
                        <span class="n">inline_has_add_permission</span> <span class="ow">or</span>
                        <span class="n">inline</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)):</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">inline_has_add_permission</span><span class="p">:</span>
                    <span class="n">inline</span><span class="o">.</span><span class="n">max_num</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">inline_instances</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inline</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">inline_instances</span></div>

<div class="viewcode-block" id="ModelAdmin.get_urls"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_urls">[docs]</a>    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">path</span>

        <span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">view</span><span class="p">):</span>
            <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="n">view</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">wrapper</span><span class="o">.</span><span class="n">model_admin</span> <span class="o">=</span> <span class="bp">self</span>
            <span class="k">return</span> <span class="n">update_wrapper</span><span class="p">(</span><span class="n">wrapper</span><span class="p">,</span> <span class="n">view</span><span class="p">)</span>

        <span class="n">info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>

        <span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">changelist_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_changelist&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;add/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">add_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_add&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;autocomplete/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">autocomplete_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_autocomplete&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;path:object_id&gt;/history/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">history_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_history&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;path:object_id&gt;/delete/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">delete_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_delete&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;path:object_id&gt;/change/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">change_view</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_change&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">),</span>
            <span class="c1"># For backwards compatibility (was the change url before 1.9)</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;path:object_id&gt;/&#39;</span><span class="p">,</span> <span class="n">wrap</span><span class="p">(</span><span class="n">RedirectView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span>
                <span class="n">pattern_name</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_change&#39;</span> <span class="o">%</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">,)</span> <span class="o">+</span> <span class="n">info</span><span class="p">)</span>
            <span class="p">))),</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">urlpatterns</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_urls</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">extra</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span> <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span> <span class="k">else</span> <span class="s1">&#39;.min&#39;</span>
        <span class="n">js</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s1">&#39;vendor/jquery/jquery</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">,</span>
            <span class="s1">&#39;jquery.init.js&#39;</span><span class="p">,</span>
            <span class="s1">&#39;core.js&#39;</span><span class="p">,</span>
            <span class="s1">&#39;admin/RelatedObjectLookups.js&#39;</span><span class="p">,</span>
            <span class="s1">&#39;actions</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">,</span>
            <span class="s1">&#39;urlify.js&#39;</span><span class="p">,</span>
            <span class="s1">&#39;prepopulate</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">,</span>
            <span class="s1">&#39;vendor/xregexp/xregexp</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">,</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">forms</span><span class="o">.</span><span class="n">Media</span><span class="p">(</span><span class="n">js</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;admin/js/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">url</span> <span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">js</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">get_model_perms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dict of all perms for this model. This dict has the keys</span>
<span class="sd">        ``add``, ``change``, ``delete``, and ``view`` mapping to the True/False</span>
<span class="sd">        for each of those actions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;add&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;change&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;delete&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;view&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_get_form_for_get_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.get_form"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_form">[docs]</a>    <span class="k">def</span> <span class="nf">get_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a Form class for use in the admin add view. This is used by</span>
<span class="sd">        add_view and change_view.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s1">&#39;fields&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;fields&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">flatten_fieldsets</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_fieldsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
        <span class="n">excluded</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_exclude</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">excluded</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="nb">list</span><span class="p">(</span><span class="n">excluded</span><span class="p">)</span>
        <span class="n">readonly_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_readonly_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="n">exclude</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">readonly_fields</span><span class="p">)</span>
        <span class="c1"># Exclude all fields if it&#39;s a change form and the user doesn&#39;t have</span>
        <span class="c1"># the change permission.</span>
        <span class="k">if</span> <span class="n">change</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;user&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="n">exclude</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">excluded</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span><span class="p">:</span>
            <span class="c1"># Take the custom ModelForm&#39;s Meta.exclude into account only if the</span>
            <span class="c1"># ModelAdmin doesn&#39;t define its own.</span>
            <span class="n">exclude</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span><span class="p">)</span>
        <span class="c1"># if exclude is an empty list we pass None to be consistent with the</span>
        <span class="c1"># default on modelform_factory</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="n">exclude</span> <span class="ow">or</span> <span class="kc">None</span>

        <span class="c1"># Remove declared form fields which are in readonly_fields.</span>
        <span class="n">new_attrs</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="o">.</span><span class="n">fromkeys</span><span class="p">(</span>
            <span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">readonly_fields</span>
            <span class="k">if</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">declared_fields</span>
        <span class="p">)</span>
        <span class="n">form</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">,),</span> <span class="n">new_attrs</span><span class="p">)</span>

        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="n">fields</span><span class="p">,</span>
            <span class="s1">&#39;exclude&#39;</span><span class="p">:</span> <span class="n">exclude</span><span class="p">,</span>
            <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_dbfield</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">),</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">modelform_defines_fields</span><span class="p">(</span><span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]):</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ALL_FIELDS</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">modelform_factory</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">defaults</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">FieldError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">FieldError</span><span class="p">(</span>
                <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">. Check fields/fieldsets/exclude attributes of class </span><span class="si">%s</span><span class="s1">.&#39;</span>
                <span class="o">%</span> <span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.get_changelist"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_changelist">[docs]</a>    <span class="k">def</span> <span class="nf">get_changelist</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the ChangeList class for use on the changelist page.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.views.main</span> <span class="k">import</span> <span class="n">ChangeList</span>
        <span class="k">return</span> <span class="n">ChangeList</span></div>

    <span class="k">def</span> <span class="nf">get_changelist_instance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a `ChangeList` instance based on `request`. May raise</span>
<span class="sd">        `IncorrectLookupParameters`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">list_display</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_list_display</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">list_display_links</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_list_display_links</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">list_display</span><span class="p">)</span>
        <span class="c1"># Add the action checkboxes if any actions are available.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_actions</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="n">list_display</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;action_checkbox&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">list_display</span><span class="p">]</span>
        <span class="n">sortable_by</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_sortable_by</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">ChangeList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changelist</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ChangeList</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
            <span class="n">list_display</span><span class="p">,</span>
            <span class="n">list_display_links</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_list_filter</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">date_hierarchy</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_search_fields</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_list_select_related</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">list_per_page</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">list_max_show_all</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">list_editable</span><span class="p">,</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">sortable_by</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">from_field</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return an instance matching the field and value provided, the primary</span>
<span class="sd">        key is used if no field is provided. Return ``None`` if no match is</span>
<span class="sd">        found or the object_id fails validation.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">model</span>
        <span class="n">field</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span> <span class="k">if</span> <span class="n">from_field</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">from_field</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">object_id</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">to_python</span><span class="p">(</span><span class="n">object_id</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">object_id</span><span class="p">})</span>
        <span class="k">except</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">None</span>

<div class="viewcode-block" id="ModelAdmin.get_changelist_form"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_changelist_form">[docs]</a>    <span class="k">def</span> <span class="nf">get_changelist_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a Form class for use in the Formset on the changelist page.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_dbfield</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">),</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="n">defaults</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;fields&#39;</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">modelform_defines_fields</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;form&#39;</span><span class="p">)):</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ALL_FIELDS</span>

        <span class="k">return</span> <span class="n">modelform_factory</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">defaults</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.get_changelist_formset"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_changelist_formset">[docs]</a>    <span class="k">def</span> <span class="nf">get_changelist_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a FormSet class for use on the changelist page if list_editable</span>
<span class="sd">        is used.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_dbfield</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">),</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">modelformset_factory</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changelist_form</span><span class="p">(</span><span class="n">request</span><span class="p">),</span> <span class="n">extra</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
            <span class="n">fields</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">list_editable</span><span class="p">,</span> <span class="o">**</span><span class="n">defaults</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.get_formsets_with_inlines"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_formsets_with_inlines">[docs]</a>    <span class="k">def</span> <span class="nf">get_formsets_with_inlines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Yield formsets and the corresponding inlines.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_inline_instances</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">inline</span><span class="o">.</span><span class="n">get_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span> <span class="n">inline</span></div>

<div class="viewcode-block" id="ModelAdmin.get_paginator"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_paginator">[docs]</a>    <span class="k">def</span> <span class="nf">get_paginator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">per_page</span><span class="p">,</span> <span class="n">orphans</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">allow_empty_first_page</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">paginator</span><span class="p">(</span><span class="n">queryset</span><span class="p">,</span> <span class="n">per_page</span><span class="p">,</span> <span class="n">orphans</span><span class="p">,</span> <span class="n">allow_empty_first_page</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">log_addition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="nb">object</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Log that an object has been successfully added.</span>

<span class="sd">        The default implementation creates an admin LogEntry object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="k">import</span> <span class="n">LogEntry</span><span class="p">,</span> <span class="n">ADDITION</span>
        <span class="k">return</span> <span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">log_action</span><span class="p">(</span>
            <span class="n">user_id</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">content_type_id</span><span class="o">=</span><span class="n">get_content_type_for_model</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_id</span><span class="o">=</span><span class="nb">object</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_repr</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="nb">object</span><span class="p">),</span>
            <span class="n">action_flag</span><span class="o">=</span><span class="n">ADDITION</span><span class="p">,</span>
            <span class="n">change_message</span><span class="o">=</span><span class="n">message</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">log_change</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="nb">object</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Log that an object has been successfully changed.</span>

<span class="sd">        The default implementation creates an admin LogEntry object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="k">import</span> <span class="n">LogEntry</span><span class="p">,</span> <span class="n">CHANGE</span>
        <span class="k">return</span> <span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">log_action</span><span class="p">(</span>
            <span class="n">user_id</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">content_type_id</span><span class="o">=</span><span class="n">get_content_type_for_model</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_id</span><span class="o">=</span><span class="nb">object</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_repr</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="nb">object</span><span class="p">),</span>
            <span class="n">action_flag</span><span class="o">=</span><span class="n">CHANGE</span><span class="p">,</span>
            <span class="n">change_message</span><span class="o">=</span><span class="n">message</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">log_deletion</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="nb">object</span><span class="p">,</span> <span class="n">object_repr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Log that an object will be deleted. Note that this method must be</span>
<span class="sd">        called before the deletion.</span>

<span class="sd">        The default implementation creates an admin LogEntry object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="k">import</span> <span class="n">LogEntry</span><span class="p">,</span> <span class="n">DELETION</span>
        <span class="k">return</span> <span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">log_action</span><span class="p">(</span>
            <span class="n">user_id</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">content_type_id</span><span class="o">=</span><span class="n">get_content_type_for_model</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_id</span><span class="o">=</span><span class="nb">object</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="n">object_repr</span><span class="o">=</span><span class="n">object_repr</span><span class="p">,</span>
            <span class="n">action_flag</span><span class="o">=</span><span class="n">DELETION</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">action_checkbox</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A list_display column containing a checkbox widget.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">helpers</span><span class="o">.</span><span class="n">checkbox</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">helpers</span><span class="o">.</span><span class="n">ACTION_CHECKBOX_NAME</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span><span class="p">))</span>
    <span class="n">action_checkbox</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;&lt;input type=&quot;checkbox&quot; id=&quot;action-toggle&quot;&gt;&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_base_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the list of actions, prior to any request-based filtering.&quot;&quot;&quot;</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Gather actions from the admin site first</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">actions</span><span class="p">:</span>
            <span class="n">description</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="s1">&#39;short_description&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="p">))</span>
            <span class="n">actions</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">func</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">description</span><span class="p">))</span>
        <span class="c1"># Add actions from this ModelAdmin.</span>
        <span class="n">actions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_action</span><span class="p">(</span><span class="n">action</span><span class="p">)</span> <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">actions</span> <span class="ow">or</span> <span class="p">[])</span>
        <span class="c1"># get_action might have returned None, so filter any of those out.</span>
        <span class="k">return</span> <span class="nb">filter</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">actions</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_filter_actions_by_permissions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">actions</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Filter out any actions that the user doesn&#39;t have access to.&quot;&quot;&quot;</span>
        <span class="n">filtered_actions</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
            <span class="n">callable</span> <span class="o">=</span> <span class="n">action</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">callable</span><span class="p">,</span> <span class="s1">&#39;allowed_permissions&#39;</span><span class="p">):</span>
                <span class="n">filtered_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="n">permission_checks</span> <span class="o">=</span> <span class="p">(</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;has_</span><span class="si">%s</span><span class="s1">_permission&#39;</span> <span class="o">%</span> <span class="n">permission</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">permission</span> <span class="ow">in</span> <span class="n">callable</span><span class="o">.</span><span class="n">allowed_permissions</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">has_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="k">for</span> <span class="n">has_permission</span> <span class="ow">in</span> <span class="n">permission_checks</span><span class="p">):</span>
                <span class="n">filtered_actions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">filtered_actions</span>

<div class="viewcode-block" id="ModelAdmin.get_actions"><a class="viewcode-back" href="../../../../ref/contrib/admin/actions.html#django.contrib.admin.ModelAdmin.get_actions">[docs]</a>    <span class="k">def</span> <span class="nf">get_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary mapping the names of all actions for this</span>
<span class="sd">        ModelAdmin to a tuple of (callable, name, description) for each action.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If self.actions is set to None that means actions are disabled on</span>
        <span class="c1"># this page.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">actions</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filter_actions_by_permissions</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_base_actions</span><span class="p">())</span>
        <span class="c1"># Convert the actions into an OrderedDict keyed by name.</span>
        <span class="k">return</span> <span class="n">OrderedDict</span><span class="p">(</span>
            <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">desc</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">func</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">desc</span> <span class="ow">in</span> <span class="n">actions</span>
        <span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">get_action_choices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">default_choices</span><span class="o">=</span><span class="n">BLANK_CHOICE_DASH</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of choices for use in a form object.  Each choice is a</span>
<span class="sd">        tuple (name, description).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">choices</span> <span class="o">=</span> <span class="p">[]</span> <span class="o">+</span> <span class="n">default_choices</span>
        <span class="k">for</span> <span class="n">func</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">description</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_actions</span><span class="p">(</span><span class="n">request</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">choice</span> <span class="o">=</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">description</span> <span class="o">%</span> <span class="n">model_format_dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">))</span>
            <span class="n">choices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">choices</span>

    <span class="k">def</span> <span class="nf">get_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a given action from a parameter, which can either be a callable,</span>
<span class="sd">        or the name of a method on the ModelAdmin.  Return is a tuple of</span>
<span class="sd">        (callable, name, description).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># If the action is a callable, just use it.</span>
        <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">action</span><span class="p">):</span>
            <span class="n">func</span> <span class="o">=</span> <span class="n">action</span>
            <span class="n">action</span> <span class="o">=</span> <span class="n">action</span><span class="o">.</span><span class="vm">__name__</span>

        <span class="c1"># Next, look for a method. Grab it off self.__class__ to get an unbound</span>
        <span class="c1"># method instead of a bound one; this ensures that the calling</span>
        <span class="c1"># conventions are the same for functions and methods.</span>
        <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span>
            <span class="n">func</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">action</span><span class="p">)</span>

        <span class="c1"># Finally, look for a named method on the admin site</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">get_action</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="s1">&#39;short_description&#39;</span><span class="p">):</span>
            <span class="n">description</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="n">short_description</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">description</span> <span class="o">=</span> <span class="n">capfirst</span><span class="p">(</span><span class="n">action</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">func</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">description</span>

<div class="viewcode-block" id="ModelAdmin.get_list_display"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_list_display">[docs]</a>    <span class="k">def</span> <span class="nf">get_list_display</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a sequence containing the fields to be displayed on the</span>
<span class="sd">        changelist.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_display</span></div>

<div class="viewcode-block" id="ModelAdmin.get_list_display_links"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_list_display_links">[docs]</a>    <span class="k">def</span> <span class="nf">get_list_display_links</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">list_display</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a sequence containing the fields to be displayed as links</span>
<span class="sd">        on the changelist. The list_display parameter is the list of fields</span>
<span class="sd">        returned by get_list_display().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_display_links</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_display_links</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">list_display</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_display_links</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Use only the first item in list_display as link</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">list_display</span><span class="p">)[:</span><span class="mi">1</span><span class="p">]</span></div>

<div class="viewcode-block" id="ModelAdmin.get_list_filter"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_list_filter">[docs]</a>    <span class="k">def</span> <span class="nf">get_list_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a sequence containing the fields to be displayed as filters in</span>
<span class="sd">        the right sidebar of the changelist page.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_filter</span></div>

<div class="viewcode-block" id="ModelAdmin.get_list_select_related"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_list_select_related">[docs]</a>    <span class="k">def</span> <span class="nf">get_list_select_related</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of fields to add to the select_related() part of the</span>
<span class="sd">        changelist items query.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">list_select_related</span></div>

<div class="viewcode-block" id="ModelAdmin.get_search_fields"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_search_fields">[docs]</a>    <span class="k">def</span> <span class="nf">get_search_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a sequence containing the fields to be searched whenever</span>
<span class="sd">        somebody submits a search query.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">search_fields</span></div>

<div class="viewcode-block" id="ModelAdmin.get_search_results"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_search_results">[docs]</a>    <span class="k">def</span> <span class="nf">get_search_results</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">search_term</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a tuple containing a queryset to implement the search</span>
<span class="sd">        and a boolean indicating if the results may contain duplicates.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Apply keyword searches.</span>
        <span class="k">def</span> <span class="nf">construct_search</span><span class="p">(</span><span class="n">field_name</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">field_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;^&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__istartswith&quot;</span> <span class="o">%</span> <span class="n">field_name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">elif</span> <span class="n">field_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__iexact&quot;</span> <span class="o">%</span> <span class="n">field_name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">elif</span> <span class="n">field_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__search&quot;</span> <span class="o">%</span> <span class="n">field_name</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="c1"># Use field_name if it includes a lookup.</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
            <span class="n">lookup_fields</span> <span class="o">=</span> <span class="n">field_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">)</span>
            <span class="c1"># Go through the fields, following all relations.</span>
            <span class="n">prev_field</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">path_part</span> <span class="ow">in</span> <span class="n">lookup_fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">path_part</span> <span class="o">==</span> <span class="s1">&#39;pk&#39;</span><span class="p">:</span>
                    <span class="n">path_part</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">field</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">path_part</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
                    <span class="c1"># Use valid query lookups.</span>
                    <span class="k">if</span> <span class="n">prev_field</span> <span class="ow">and</span> <span class="n">prev_field</span><span class="o">.</span><span class="n">get_lookup</span><span class="p">(</span><span class="n">path_part</span><span class="p">):</span>
                        <span class="k">return</span> <span class="n">field_name</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">prev_field</span> <span class="o">=</span> <span class="n">field</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;get_path_info&#39;</span><span class="p">):</span>
                        <span class="c1"># Update opts to follow the relation.</span>
                        <span class="n">opts</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_path_info</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">to_opts</span>
            <span class="c1"># Otherwise, use the field with icontains.</span>
            <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__icontains&quot;</span> <span class="o">%</span> <span class="n">field_name</span>

        <span class="n">use_distinct</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">search_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_search_fields</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">search_fields</span> <span class="ow">and</span> <span class="n">search_term</span><span class="p">:</span>
            <span class="n">orm_lookups</span> <span class="o">=</span> <span class="p">[</span><span class="n">construct_search</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">search_field</span><span class="p">))</span>
                           <span class="k">for</span> <span class="n">search_field</span> <span class="ow">in</span> <span class="n">search_fields</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">bit</span> <span class="ow">in</span> <span class="n">search_term</span><span class="o">.</span><span class="n">split</span><span class="p">():</span>
                <span class="n">or_queries</span> <span class="o">=</span> <span class="p">[</span><span class="n">models</span><span class="o">.</span><span class="n">Q</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">orm_lookup</span><span class="p">:</span> <span class="n">bit</span><span class="p">})</span>
                              <span class="k">for</span> <span class="n">orm_lookup</span> <span class="ow">in</span> <span class="n">orm_lookups</span><span class="p">]</span>
                <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">or_</span><span class="p">,</span> <span class="n">or_queries</span><span class="p">))</span>
            <span class="n">use_distinct</span> <span class="o">|=</span> <span class="nb">any</span><span class="p">(</span><span class="n">lookup_needs_distinct</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">,</span> <span class="n">search_spec</span><span class="p">)</span> <span class="k">for</span> <span class="n">search_spec</span> <span class="ow">in</span> <span class="n">orm_lookups</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">use_distinct</span></div>

    <span class="k">def</span> <span class="nf">get_preserved_filters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the preserved filters querystring.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">match</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">preserve_filters</span> <span class="ow">and</span> <span class="n">match</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
            <span class="n">current_url</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">app_name</span><span class="p">,</span> <span class="n">match</span><span class="o">.</span><span class="n">url_name</span><span class="p">)</span>
            <span class="n">changelist_url</span> <span class="o">=</span> <span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_changelist&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">current_url</span> <span class="o">==</span> <span class="n">changelist_url</span><span class="p">:</span>
                <span class="n">preserved_filters</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">urlencode</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">preserved_filters</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;_changelist_filters&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">preserved_filters</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">urlencode</span><span class="p">({</span><span class="s1">&#39;_changelist_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">})</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">construct_change_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">add</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Construct a JSON structure describing changes from a changed object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">construct_change_message</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">add</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.message_user"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.message_user">[docs]</a>    <span class="k">def</span> <span class="nf">message_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">messages</span><span class="o">.</span><span class="n">INFO</span><span class="p">,</span> <span class="n">extra_tags</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                     <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Send a message to the user. The default implementation</span>
<span class="sd">        posts a message using the django.contrib.messages backend.</span>

<span class="sd">        Exposes almost the same API as messages.add_message(), but accepts the</span>
<span class="sd">        positional arguments in a different order to maintain backwards</span>
<span class="sd">        compatibility. For convenience, it accepts the `level` argument as</span>
<span class="sd">        a string rather than the usual level number.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
            <span class="c1"># attempt to get the level if passed a string</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">level</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">messages</span><span class="o">.</span><span class="n">constants</span><span class="p">,</span> <span class="n">level</span><span class="o">.</span><span class="n">upper</span><span class="p">())</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="n">levels</span> <span class="o">=</span> <span class="n">messages</span><span class="o">.</span><span class="n">constants</span><span class="o">.</span><span class="n">DEFAULT_TAGS</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                <span class="n">levels_repr</span> <span class="o">=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;`</span><span class="si">%s</span><span class="s1">`&#39;</span> <span class="o">%</span> <span class="n">l</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">levels</span><span class="p">)</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s1">&#39;Bad message level string: `</span><span class="si">%s</span><span class="s1">`. Possible values are: </span><span class="si">%s</span><span class="s1">&#39;</span>
                    <span class="o">%</span> <span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="n">levels_repr</span><span class="p">)</span>
                <span class="p">)</span>

        <span class="n">messages</span><span class="o">.</span><span class="n">add_message</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">level</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">extra_tags</span><span class="o">=</span><span class="n">extra_tags</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">save_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a ModelForm return an unsaved instance. ``change`` is True if</span>
<span class="sd">        the object is being changed, and False if it&#39;s being added.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.save_model"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.save_model">[docs]</a>    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a model instance save it to the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelAdmin.delete_model"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.delete_model">[docs]</a>    <span class="k">def</span> <span class="nf">delete_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a model instance delete it from the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelAdmin.delete_queryset"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.delete_queryset">[docs]</a>    <span class="k">def</span> <span class="nf">delete_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Given a queryset, delete it from the database.&quot;&quot;&quot;</span>
        <span class="n">queryset</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelAdmin.save_formset"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.save_formset">[docs]</a>    <span class="k">def</span> <span class="nf">save_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given an inline formset save it to the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">formset</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelAdmin.save_related"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.save_related">[docs]</a>    <span class="k">def</span> <span class="nf">save_related</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given the ``HttpRequest``, the parent ``ModelForm`` instance, the</span>
<span class="sd">        list of inline formsets and a boolean value based on whether the</span>
<span class="sd">        parent is being added or changed, save the related objects to the</span>
<span class="sd">        database. Note that at this point save_form() and save_model() have</span>
<span class="sd">        already been called.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">form</span><span class="o">.</span><span class="n">save_m2m</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">formset</span> <span class="ow">in</span> <span class="n">formsets</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">save_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="n">change</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">render_change_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">add</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span>
        <span class="n">preserved_filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">form_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">form_url</span><span class="p">)</span>
        <span class="n">view_on_site_url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_view_on_site_url</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="n">has_editable_inline_admin_formsets</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;inline_admin_formsets&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_add_permission</span> <span class="ow">or</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_change_permission</span> <span class="ow">or</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">:</span>
                <span class="n">has_editable_inline_admin_formsets</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">break</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="s1">&#39;add&#39;</span><span class="p">:</span> <span class="n">add</span><span class="p">,</span>
            <span class="s1">&#39;change&#39;</span><span class="p">:</span> <span class="n">change</span><span class="p">,</span>
            <span class="s1">&#39;has_view_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span>
            <span class="s1">&#39;has_add_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;has_change_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span>
            <span class="s1">&#39;has_delete_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span>
            <span class="s1">&#39;has_editable_inline_admin_formsets&#39;</span><span class="p">:</span> <span class="n">has_editable_inline_admin_formsets</span><span class="p">,</span>
            <span class="s1">&#39;has_file_field&#39;</span><span class="p">:</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;adminform&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">is_multipart</span><span class="p">()</span> <span class="ow">or</span> <span class="nb">any</span><span class="p">(</span>
                <span class="n">admin_formset</span><span class="o">.</span><span class="n">formset</span><span class="o">.</span><span class="n">form</span><span class="p">()</span><span class="o">.</span><span class="n">is_multipart</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">admin_formset</span> <span class="ow">in</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;inline_admin_formsets&#39;</span><span class="p">]</span>
            <span class="p">),</span>
            <span class="s1">&#39;has_absolute_url&#39;</span><span class="p">:</span> <span class="n">view_on_site_url</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s1">&#39;absolute_url&#39;</span><span class="p">:</span> <span class="n">view_on_site_url</span><span class="p">,</span>
            <span class="s1">&#39;form_url&#39;</span><span class="p">:</span> <span class="n">form_url</span><span class="p">,</span>
            <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">,</span>
            <span class="s1">&#39;content_type_id&#39;</span><span class="p">:</span> <span class="n">get_content_type_for_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
            <span class="s1">&#39;save_as&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_as</span><span class="p">,</span>
            <span class="s1">&#39;save_on_top&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_on_top</span><span class="p">,</span>
            <span class="s1">&#39;to_field_var&#39;</span><span class="p">:</span> <span class="n">TO_FIELD_VAR</span><span class="p">,</span>
            <span class="s1">&#39;is_popup_var&#39;</span><span class="p">:</span> <span class="n">IS_POPUP_VAR</span><span class="p">,</span>
            <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">app_label</span><span class="p">,</span>
        <span class="p">})</span>
        <span class="k">if</span> <span class="n">add</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_form_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">form_template</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_form_template</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">form_template</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">change_form_template</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form_template</span> <span class="ow">or</span> <span class="p">[</span>
            <span class="s2">&quot;admin/</span><span class="si">%s</span><span class="s2">/</span><span class="si">%s</span><span class="s2">/change_form.html&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
            <span class="s2">&quot;admin/</span><span class="si">%s</span><span class="s2">/change_form.html&quot;</span> <span class="o">%</span> <span class="n">app_label</span><span class="p">,</span>
            <span class="s2">&quot;admin/change_form.html&quot;</span>
        <span class="p">],</span> <span class="n">context</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.response_add"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.response_add">[docs]</a>    <span class="k">def</span> <span class="nf">response_add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">post_url_continue</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the HttpResponse for the add_view stage.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">preserved_filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">obj_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span>
            <span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_change&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
            <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">quote</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span><span class="p">),),</span>
            <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="c1"># Add a link to the object&#39;s change form if the user can edit the obj.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="n">obj_repr</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span><span class="s1">&#39;&lt;a href=&quot;</span><span class="si">{}</span><span class="s1">&quot;&gt;</span><span class="si">{}</span><span class="s1">&lt;/a&gt;&#39;</span><span class="p">,</span> <span class="n">urlquote</span><span class="p">(</span><span class="n">obj_url</span><span class="p">),</span> <span class="n">obj</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj_repr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="n">msg_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
            <span class="s1">&#39;obj&#39;</span><span class="p">:</span> <span class="n">obj_repr</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="c1"># Here, we distinguish between different save types by checking for</span>
        <span class="c1"># the presence of keys in request.POST.</span>

        <span class="k">if</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">to_field</span><span class="p">:</span>
                <span class="n">attr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">to_field</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">attr</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">serializable_value</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="n">popup_response_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span>
                <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">),</span>
                <span class="s1">&#39;obj&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">),</span>
            <span class="p">})</span>
            <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">popup_response_template</span> <span class="ow">or</span> <span class="p">[</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="s1">&#39;admin/popup_response.html&#39;</span><span class="p">,</span>
            <span class="p">],</span> <span class="p">{</span>
                <span class="s1">&#39;popup_response_data&#39;</span><span class="p">:</span> <span class="n">popup_response_data</span><span class="p">,</span>
            <span class="p">})</span>

        <span class="k">elif</span> <span class="s2">&quot;_continue&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">or</span> <span class="p">(</span>
                <span class="c1"># Redirecting after &quot;Save as new&quot;.</span>
                <span class="s2">&quot;_saveasnew&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_as_continue</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="p">):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was added successfully.&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
                <span class="n">msg</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;You may edit it again below.&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">format_html</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">**</span><span class="n">msg_dict</span><span class="p">),</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">post_url_continue</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">post_url_continue</span> <span class="o">=</span> <span class="n">obj_url</span>
            <span class="n">post_url_continue</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">(</span>
                <span class="p">{</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span>
                <span class="n">post_url_continue</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_url_continue</span><span class="p">)</span>

        <span class="k">elif</span> <span class="s2">&quot;_addanother&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was added successfully. You may add another </span><span class="si">{name}</span><span class="s1"> below.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">redirect_url</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">redirect_url</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was added successfully.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_post_save_add</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.response_change"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.response_change">[docs]</a>    <span class="k">def</span> <span class="nf">response_change</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the HttpResponse for the change_view stage.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">opts</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">_meta</span>
            <span class="n">to_field</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">)</span>
            <span class="n">attr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">to_field</span><span class="p">)</span> <span class="k">if</span> <span class="n">to_field</span> <span class="k">else</span> <span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">resolver_match</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;object_id&#39;</span><span class="p">]</span>
            <span class="n">new_value</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">serializable_value</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="n">popup_response_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span>
                <span class="s1">&#39;action&#39;</span><span class="p">:</span> <span class="s1">&#39;change&#39;</span><span class="p">,</span>
                <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">),</span>
                <span class="s1">&#39;obj&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">),</span>
                <span class="s1">&#39;new_value&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_value</span><span class="p">),</span>
            <span class="p">})</span>
            <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">popup_response_template</span> <span class="ow">or</span> <span class="p">[</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="s1">&#39;admin/popup_response.html&#39;</span><span class="p">,</span>
            <span class="p">],</span> <span class="p">{</span>
                <span class="s1">&#39;popup_response_data&#39;</span><span class="p">:</span> <span class="n">popup_response_data</span><span class="p">,</span>
            <span class="p">})</span>

        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">preserved_filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="n">msg_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
            <span class="s1">&#39;obj&#39;</span><span class="p">:</span> <span class="n">format_html</span><span class="p">(</span><span class="s1">&#39;&lt;a href=&quot;</span><span class="si">{}</span><span class="s1">&quot;&gt;</span><span class="si">{}</span><span class="s1">&lt;/a&gt;&#39;</span><span class="p">,</span> <span class="n">urlquote</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">),</span> <span class="n">obj</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="s2">&quot;_continue&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was changed successfully. You may edit it again below.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">redirect_url</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">redirect_url</span><span class="p">)</span>

        <span class="k">elif</span> <span class="s2">&quot;_saveasnew&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was added successfully. You may edit it again below.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_change&#39;</span> <span class="o">%</span>
                                   <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                                   <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span><span class="p">,),</span>
                                   <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">redirect_url</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">redirect_url</span><span class="p">)</span>

        <span class="k">elif</span> <span class="s2">&quot;_addanother&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was changed successfully. You may add another </span><span class="si">{name}</span><span class="s1"> below.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_add&#39;</span> <span class="o">%</span>
                                   <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                                   <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">redirect_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">redirect_url</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">redirect_url</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">format_html</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">{name}</span><span class="s1"> &quot;</span><span class="si">{obj}</span><span class="s1">&quot; was changed successfully.&#39;</span><span class="p">),</span>
                <span class="o">**</span><span class="n">msg_dict</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_post_save_change</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_response_post_save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_changelist&#39;</span> <span class="o">%</span>
                               <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                               <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">preserved_filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">({</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">post_url</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span>
                               <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_url</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">response_post_save_add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Figure out where to redirect after the &#39;Save&#39; button has been pressed</span>
<span class="sd">        when adding a new object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_response_post_save</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">response_post_save_change</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Figure out where to redirect after the &#39;Save&#39; button has been pressed</span>
<span class="sd">        when editing an existing object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_response_post_save</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">response_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Handle an admin action. This is called if a request is POSTed to the</span>
<span class="sd">        changelist; it returns an HttpResponse if the action was handled, and</span>
<span class="sd">        None otherwise.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># There can be multiple action forms on the page (at the top</span>
        <span class="c1"># and bottom of the change list, for example). Get the action</span>
        <span class="c1"># whose button was pushed.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">action_index</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;index&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="n">action_index</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="c1"># Construct the action form.</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">data</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">helpers</span><span class="o">.</span><span class="n">ACTION_CHECKBOX_NAME</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">data</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;index&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="c1"># Use the action whose button was pushed</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;action&#39;</span><span class="p">:</span> <span class="n">data</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="s1">&#39;action&#39;</span><span class="p">)[</span><span class="n">action_index</span><span class="p">]})</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="c1"># If we didn&#39;t get an action from the chosen form that&#39;s invalid</span>
            <span class="c1"># POST data, so by deleting action it&#39;ll fail the validation check</span>
            <span class="c1"># below. So no need to do anything here</span>
            <span class="k">pass</span>

        <span class="n">action_form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">action_form</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">auto_id</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="n">action_form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_action_choices</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="c1"># If the form&#39;s valid we can handle the action.</span>
        <span class="k">if</span> <span class="n">action_form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="n">action</span> <span class="o">=</span> <span class="n">action_form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span>
            <span class="n">select_across</span> <span class="o">=</span> <span class="n">action_form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s1">&#39;select_across&#39;</span><span class="p">]</span>
            <span class="n">func</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_actions</span><span class="p">(</span><span class="n">request</span><span class="p">)[</span><span class="n">action</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>

            <span class="c1"># Get the list of selected PKs. If nothing&#39;s selected, we can&#39;t</span>
            <span class="c1"># perform an action on it, so bail. Except we want to perform</span>
            <span class="c1"># the action explicitly on all objects.</span>
            <span class="n">selected</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="n">helpers</span><span class="o">.</span><span class="n">ACTION_CHECKBOX_NAME</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">selected</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">select_across</span><span class="p">:</span>
                <span class="c1"># Reminder that something needs to be selected or nothing will happen</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Items must be selected in order to perform &quot;</span>
                        <span class="s2">&quot;actions on them. No items have been changed.&quot;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">select_across</span><span class="p">:</span>
                <span class="c1"># Perform the action only on the selected objects</span>
                <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="n">selected</span><span class="p">)</span>

            <span class="n">response</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">)</span>

            <span class="c1"># Actions may return an HttpResponse-like object, which will be</span>
            <span class="c1"># used as the response from the POST. If not, we&#39;ll be a good</span>
            <span class="c1"># little HTTP citizen and redirect back to the changelist page.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="n">HttpResponseBase</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">response</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;No action selected.&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>

<div class="viewcode-block" id="ModelAdmin.response_delete"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.response_delete">[docs]</a>    <span class="k">def</span> <span class="nf">response_delete</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj_display</span><span class="p">,</span> <span class="n">obj_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the HttpResponse for the delete_view stage.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">if</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">popup_response_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span>
                <span class="s1">&#39;action&#39;</span><span class="p">:</span> <span class="s1">&#39;delete&#39;</span><span class="p">,</span>
                <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj_id</span><span class="p">),</span>
            <span class="p">})</span>
            <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">popup_response_template</span> <span class="ow">or</span> <span class="p">[</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/popup_response.html&#39;</span> <span class="o">%</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                <span class="s1">&#39;admin/popup_response.html&#39;</span><span class="p">,</span>
            <span class="p">],</span> <span class="p">{</span>
                <span class="s1">&#39;popup_response_data&#39;</span><span class="p">:</span> <span class="n">popup_response_data</span><span class="p">,</span>
            <span class="p">})</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span>
            <span class="n">_</span><span class="p">(</span><span class="s1">&#39;The </span><span class="si">%(name)s</span><span class="s1"> &quot;</span><span class="si">%(obj)s</span><span class="s1">&quot; was deleted successfully.&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
                <span class="s1">&#39;obj&#39;</span><span class="p">:</span> <span class="n">obj_display</span><span class="p">,</span>
            <span class="p">},</span>
            <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span>
                <span class="s1">&#39;admin:</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">_changelist&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">preserved_filters</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">add_preserved_filters</span><span class="p">(</span>
                <span class="p">{</span><span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="n">preserved_filters</span><span class="p">,</span> <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">},</span> <span class="n">post_url</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">post_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">post_url</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">render_delete_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="n">to_field_var</span><span class="o">=</span><span class="n">TO_FIELD_VAR</span><span class="p">,</span>
            <span class="n">is_popup_var</span><span class="o">=</span><span class="n">IS_POPUP_VAR</span><span class="p">,</span>
            <span class="n">media</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">media</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delete_confirmation_template</span> <span class="ow">or</span> <span class="p">[</span>
                <span class="s2">&quot;admin/</span><span class="si">{}</span><span class="s2">/</span><span class="si">{}</span><span class="s2">/delete_confirmation.html&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="s2">&quot;admin/</span><span class="si">{}</span><span class="s2">/delete_confirmation.html&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">app_label</span><span class="p">),</span>
                <span class="s2">&quot;admin/delete_confirmation.html&quot;</span><span class="p">,</span>
            <span class="p">],</span>
            <span class="n">context</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_inline_formsets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Edit permissions on parent model are required for editable inlines.</span>
        <span class="n">can_edit_parent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">if</span> <span class="n">obj</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">inline_admin_formsets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">inline</span><span class="p">,</span> <span class="n">formset</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">inline_instances</span><span class="p">,</span> <span class="n">formsets</span><span class="p">):</span>
            <span class="n">fieldsets</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">inline</span><span class="o">.</span><span class="n">get_fieldsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
            <span class="n">readonly</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">inline</span><span class="o">.</span><span class="n">get_readonly_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">can_edit_parent</span><span class="p">:</span>
                <span class="n">has_add_permission</span> <span class="o">=</span> <span class="n">inline</span><span class="o">.</span><span class="n">_has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
                <span class="n">has_change_permission</span> <span class="o">=</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
                <span class="n">has_delete_permission</span> <span class="o">=</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Disable all edit-permissions, and overide formset settings.</span>
                <span class="n">has_add_permission</span> <span class="o">=</span> <span class="n">has_change_permission</span> <span class="o">=</span> <span class="n">has_delete_permission</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="n">formset</span><span class="o">.</span><span class="n">extra</span> <span class="o">=</span> <span class="n">formset</span><span class="o">.</span><span class="n">max_num</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">has_view_permission</span> <span class="o">=</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
            <span class="n">prepopulated</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">inline</span><span class="o">.</span><span class="n">get_prepopulated_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
            <span class="n">inline_admin_formset</span> <span class="o">=</span> <span class="n">helpers</span><span class="o">.</span><span class="n">InlineAdminFormSet</span><span class="p">(</span>
                <span class="n">inline</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">fieldsets</span><span class="p">,</span> <span class="n">prepopulated</span><span class="p">,</span> <span class="n">readonly</span><span class="p">,</span> <span class="n">model_admin</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
                <span class="n">has_add_permission</span><span class="o">=</span><span class="n">has_add_permission</span><span class="p">,</span> <span class="n">has_change_permission</span><span class="o">=</span><span class="n">has_change_permission</span><span class="p">,</span>
                <span class="n">has_delete_permission</span><span class="o">=</span><span class="n">has_delete_permission</span><span class="p">,</span> <span class="n">has_view_permission</span><span class="o">=</span><span class="n">has_view_permission</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">inline_admin_formsets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inline_admin_formset</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inline_admin_formsets</span>

<div class="viewcode-block" id="ModelAdmin.get_changeform_initial_data"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_changeform_initial_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_changeform_initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the initial form data from the request&#39;s GET params.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initial</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">initial</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="c1"># We have to special-case M2Ms as a list of comma-separated PKs.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">):</span>
                <span class="n">initial</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">initial</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">initial</span></div>

    <span class="k">def</span> <span class="nf">_get_obj_does_not_exist_redirect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="n">object_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a message informing the user that the object doesn&#39;t exist</span>
<span class="sd">        and return a redirect to the admin index page.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="si">%(name)s</span><span class="s2"> with ID &quot;</span><span class="si">%(key)s</span><span class="s2">&quot; doesn&#39;t exist. Perhaps it was deleted?&quot;&quot;&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
            <span class="s1">&#39;key&#39;</span><span class="p">:</span> <span class="n">unquote</span><span class="p">(</span><span class="n">object_id</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:index&#39;</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

    <span class="nd">@csrf_protect_m</span>
    <span class="k">def</span> <span class="nf">changeform_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_changeform_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_changeform_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">):</span>
        <span class="n">to_field</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">to_field</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field_allowed</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">to_field</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">DisallowedModelAdminToField</span><span class="p">(</span><span class="s2">&quot;The field </span><span class="si">%s</span><span class="s2"> cannot be referenced.&quot;</span> <span class="o">%</span> <span class="n">to_field</span><span class="p">)</span>

        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>

        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span> <span class="ow">and</span> <span class="s1">&#39;_saveasnew&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">object_id</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">add</span> <span class="o">=</span> <span class="n">object_id</span> <span class="ow">is</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">add</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">PermissionDenied</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">unquote</span><span class="p">(</span><span class="n">object_id</span><span class="p">),</span> <span class="n">to_field</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">PermissionDenied</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">PermissionDenied</span>

            <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_obj_does_not_exist_redirect</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="n">object_id</span><span class="p">)</span>

        <span class="n">ModelForm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="ow">not</span> <span class="n">add</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span><span class="p">:</span>
            <span class="n">form</span> <span class="o">=</span> <span class="n">ModelForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">,</span> <span class="n">instance</span><span class="o">=</span><span class="n">obj</span><span class="p">)</span>
            <span class="n">form_validated</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">form_validated</span><span class="p">:</span>
                <span class="n">new_object</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="ow">not</span> <span class="n">add</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">new_object</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span>
            <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_formsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="ow">not</span> <span class="n">add</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">all_valid</span><span class="p">(</span><span class="n">formsets</span><span class="p">)</span> <span class="ow">and</span> <span class="n">form_validated</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="ow">not</span> <span class="n">add</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">save_related</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="ow">not</span> <span class="n">add</span><span class="p">)</span>
                <span class="n">change_message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">construct_change_message</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">add</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">add</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log_addition</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">,</span> <span class="n">change_message</span><span class="p">)</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_add</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">log_change</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">,</span> <span class="n">change_message</span><span class="p">)</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_change</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">new_object</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">form_validated</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">add</span><span class="p">:</span>
                <span class="n">initial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changeform_initial_data</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
                <span class="n">form</span> <span class="o">=</span> <span class="n">ModelForm</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="n">initial</span><span class="p">)</span>
                <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_formsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">form</span> <span class="o">=</span> <span class="n">ModelForm</span><span class="p">(</span><span class="n">instance</span><span class="o">=</span><span class="n">obj</span><span class="p">)</span>
                <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_formsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">add</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="n">readonly_fields</span> <span class="o">=</span> <span class="n">flatten_fieldsets</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_fieldsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">readonly_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_readonly_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="n">adminForm</span> <span class="o">=</span> <span class="n">helpers</span><span class="o">.</span><span class="n">AdminForm</span><span class="p">(</span>
            <span class="n">form</span><span class="p">,</span>
            <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_fieldsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)),</span>
            <span class="c1"># Clear prepopulated fields on a view-only form to avoid a crash.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_prepopulated_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">if</span> <span class="n">add</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">else</span> <span class="p">{},</span>
            <span class="n">readonly_fields</span><span class="p">,</span>
            <span class="n">model_admin</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">media</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">media</span> <span class="o">+</span> <span class="n">adminForm</span><span class="o">.</span><span class="n">media</span>

        <span class="n">inline_formsets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_inline_formsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">inline_formset</span> <span class="ow">in</span> <span class="n">inline_formsets</span><span class="p">:</span>
            <span class="n">media</span> <span class="o">=</span> <span class="n">media</span> <span class="o">+</span> <span class="n">inline_formset</span><span class="o">.</span><span class="n">media</span>

        <span class="k">if</span> <span class="n">add</span><span class="p">:</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Add </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Change </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;View </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span> <span class="o">%</span> <span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
            <span class="s1">&#39;adminform&#39;</span><span class="p">:</span> <span class="n">adminForm</span><span class="p">,</span>
            <span class="s1">&#39;object_id&#39;</span><span class="p">:</span> <span class="n">object_id</span><span class="p">,</span>
            <span class="s1">&#39;original&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="p">,</span>
            <span class="s1">&#39;is_popup&#39;</span><span class="p">:</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">or</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">,</span>
            <span class="s1">&#39;to_field&#39;</span><span class="p">:</span> <span class="n">to_field</span><span class="p">,</span>
            <span class="s1">&#39;media&#39;</span><span class="p">:</span> <span class="n">media</span><span class="p">,</span>
            <span class="s1">&#39;inline_admin_formsets&#39;</span><span class="p">:</span> <span class="n">inline_formsets</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span><span class="p">:</span> <span class="n">helpers</span><span class="o">.</span><span class="n">AdminErrorList</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="p">),</span>
            <span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
        <span class="p">}</span>

        <span class="c1"># Hide the &quot;Save&quot; and &quot;Save and continue&quot; buttons if &quot;Save as New&quot; was</span>
        <span class="c1"># previously chosen to prevent the interface from getting confusing.</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">form_validated</span> <span class="ow">and</span> <span class="s2">&quot;_saveasnew&quot;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="n">context</span><span class="p">[</span><span class="s1">&#39;show_save&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">context</span><span class="p">[</span><span class="s1">&#39;show_save_and_continue&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="c1"># Use the change template instead of the add template.</span>
            <span class="n">add</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{})</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_change_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">add</span><span class="o">=</span><span class="n">add</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="ow">not</span> <span class="n">add</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="n">obj</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="n">form_url</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">autocomplete_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">AutocompleteJsonView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">model_admin</span><span class="o">=</span><span class="bp">self</span><span class="p">)(</span><span class="n">request</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.add_view"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.add_view">[docs]</a>    <span class="k">def</span> <span class="nf">add_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">changeform_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.change_view"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.change_view">[docs]</a>    <span class="k">def</span> <span class="nf">change_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">changeform_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_get_edited_object_pks</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return POST data values of list_editable primary keys.&quot;&quot;&quot;</span>
        <span class="n">pk_pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
            <span class="sa">r</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">-\d+-</span><span class="si">{}</span><span class="s1">$&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">prefix</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">value</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">pk_pattern</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">key</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_get_list_editable_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Based on POST data, return a queryset of the objects that were edited</span>
<span class="sd">        via list_editable.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">object_pks</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_edited_object_pks</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">prefix</span><span class="p">)</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">validate</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">to_python</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">pk</span> <span class="ow">in</span> <span class="n">object_pks</span><span class="p">:</span>
                <span class="n">validate</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
            <span class="c1"># Disable the optimization if the POST data was tampered with.</span>
            <span class="k">return</span> <span class="n">queryset</span>
        <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="n">object_pks</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.changelist_view"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.changelist_view">[docs]</a>    <span class="nd">@csrf_protect_m</span>
    <span class="k">def</span> <span class="nf">changelist_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The &#39;change list&#39; admin view for this model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.views.main</span> <span class="k">import</span> <span class="n">ERROR_FLAG</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">PermissionDenied</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">cl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changelist_instance</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">IncorrectLookupParameters</span><span class="p">:</span>
            <span class="c1"># Wacky lookup parameters were given, so redirect to the main</span>
            <span class="c1"># changelist page, without parameters, and pass an &#39;invalid=1&#39;</span>
            <span class="c1"># parameter via the query string. If wacky parameters were given</span>
            <span class="c1"># and the &#39;invalid=1&#39; parameter was already in the query string,</span>
            <span class="c1"># something is screwed up with the database, so display an error</span>
            <span class="c1"># page.</span>
            <span class="k">if</span> <span class="n">ERROR_FLAG</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">SimpleTemplateResponse</span><span class="p">(</span><span class="s1">&#39;admin/invalid_setup.html&#39;</span><span class="p">,</span> <span class="p">{</span>
                    <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Database error&#39;</span><span class="p">),</span>
                <span class="p">})</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path</span> <span class="o">+</span> <span class="s1">&#39;?&#39;</span> <span class="o">+</span> <span class="n">ERROR_FLAG</span> <span class="o">+</span> <span class="s1">&#39;=1&#39;</span><span class="p">)</span>

        <span class="c1"># If the request was POSTed, this might be a bulk action or a bulk</span>
        <span class="c1"># edit. Try to look up an action or confirmation first, but if this</span>
        <span class="c1"># isn&#39;t an action the POST will fall through to the bulk edit check,</span>
        <span class="c1"># below.</span>
        <span class="n">action_failed</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">selected</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="n">helpers</span><span class="o">.</span><span class="n">ACTION_CHECKBOX_NAME</span><span class="p">)</span>

        <span class="n">actions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_actions</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="c1"># Actions with no confirmation</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">actions</span> <span class="ow">and</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span> <span class="ow">and</span>
                <span class="s1">&#39;index&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">and</span> <span class="s1">&#39;_save&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">selected</span><span class="p">:</span>
                <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_action</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="n">cl</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">response</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">response</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">action_failed</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Items must be selected in order to perform &quot;</span>
                        <span class="s2">&quot;actions on them. No items have been changed.&quot;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>
                <span class="n">action_failed</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="c1"># Actions with confirmation</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">actions</span> <span class="ow">and</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span> <span class="ow">and</span>
                <span class="n">helpers</span><span class="o">.</span><span class="n">ACTION_CHECKBOX_NAME</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">and</span>
                <span class="s1">&#39;index&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">and</span> <span class="s1">&#39;_save&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">selected</span><span class="p">:</span>
                <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_action</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="n">cl</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">response</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">response</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">action_failed</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">if</span> <span class="n">action_failed</span><span class="p">:</span>
            <span class="c1"># Redirect back to the changelist page to avoid resubmitting the</span>
            <span class="c1"># form if the user refreshes the browser or uses the &quot;No, take</span>
            <span class="c1"># me back&quot; button on the action confirmation page.</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">())</span>

        <span class="c1"># If we&#39;re allowing changelist editing, we need to construct a formset</span>
        <span class="c1"># for the changelist given all the fields to be edited. Then we&#39;ll</span>
        <span class="c1"># use the formset to validate/process POSTed data.</span>
        <span class="n">formset</span> <span class="o">=</span> <span class="n">cl</span><span class="o">.</span><span class="n">formset</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Handle POSTed bulk-edit data.</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span> <span class="ow">and</span> <span class="n">cl</span><span class="o">.</span><span class="n">list_editable</span> <span class="ow">and</span> <span class="s1">&#39;_save&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">PermissionDenied</span>
            <span class="n">FormSet</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changelist_formset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">modified_objects</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_list_editable_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">FormSet</span><span class="o">.</span><span class="n">get_default_prefix</span><span class="p">())</span>
            <span class="n">formset</span> <span class="o">=</span> <span class="n">cl</span><span class="o">.</span><span class="n">formset</span> <span class="o">=</span> <span class="n">FormSet</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="n">modified_objects</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
                <span class="n">changecount</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">has_changed</span><span class="p">():</span>
                        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">save_related</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="o">=</span><span class="p">[],</span> <span class="n">change</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                        <span class="n">change_msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">construct_change_message</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">log_change</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">change_msg</span><span class="p">)</span>
                        <span class="n">changecount</span> <span class="o">+=</span> <span class="mi">1</span>

                <span class="k">if</span> <span class="n">changecount</span><span class="p">:</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="n">ngettext</span><span class="p">(</span>
                        <span class="s2">&quot;</span><span class="si">%(count)s</span><span class="s2"> </span><span class="si">%(name)s</span><span class="s2"> was changed successfully.&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;</span><span class="si">%(count)s</span><span class="s2"> </span><span class="si">%(name)s</span><span class="s2"> were changed successfully.&quot;</span><span class="p">,</span>
                        <span class="n">changecount</span>
                    <span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                        <span class="s1">&#39;count&#39;</span><span class="p">:</span> <span class="n">changecount</span><span class="p">,</span>
                        <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">model_ngettext</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="n">changecount</span><span class="p">),</span>
                    <span class="p">}</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">messages</span><span class="o">.</span><span class="n">SUCCESS</span><span class="p">)</span>

                <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">())</span>

        <span class="c1"># Handle GET -- construct a formset for display.</span>
        <span class="k">elif</span> <span class="n">cl</span><span class="o">.</span><span class="n">list_editable</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="n">FormSet</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_changelist_formset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">formset</span> <span class="o">=</span> <span class="n">cl</span><span class="o">.</span><span class="n">formset</span> <span class="o">=</span> <span class="n">FormSet</span><span class="p">(</span><span class="n">queryset</span><span class="o">=</span><span class="n">cl</span><span class="o">.</span><span class="n">result_list</span><span class="p">)</span>

        <span class="c1"># Build the list of media to be used by the formset.</span>
        <span class="k">if</span> <span class="n">formset</span><span class="p">:</span>
            <span class="n">media</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">media</span> <span class="o">+</span> <span class="n">formset</span><span class="o">.</span><span class="n">media</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">media</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">media</span>

        <span class="c1"># Build the action form and populate it with available actions.</span>
        <span class="k">if</span> <span class="n">actions</span><span class="p">:</span>
            <span class="n">action_form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">action_form</span><span class="p">(</span><span class="n">auto_id</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
            <span class="n">action_form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s1">&#39;action&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_action_choices</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">media</span> <span class="o">+=</span> <span class="n">action_form</span><span class="o">.</span><span class="n">media</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">action_form</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="n">selection_note_all</span> <span class="o">=</span> <span class="n">ngettext</span><span class="p">(</span>
            <span class="s1">&#39;</span><span class="si">%(total_count)s</span><span class="s1"> selected&#39;</span><span class="p">,</span>
            <span class="s1">&#39;All </span><span class="si">%(total_count)s</span><span class="s1"> selected&#39;</span><span class="p">,</span>
            <span class="n">cl</span><span class="o">.</span><span class="n">result_count</span>
        <span class="p">)</span>

        <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;module_name&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">),</span>
            <span class="s1">&#39;selection_note&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;0 of </span><span class="si">%(cnt)s</span><span class="s1"> selected&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;cnt&#39;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">cl</span><span class="o">.</span><span class="n">result_list</span><span class="p">)},</span>
            <span class="s1">&#39;selection_note_all&#39;</span><span class="p">:</span> <span class="n">selection_note_all</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;total_count&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="o">.</span><span class="n">result_count</span><span class="p">},</span>
            <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="o">.</span><span class="n">title</span><span class="p">,</span>
            <span class="s1">&#39;is_popup&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="o">.</span><span class="n">is_popup</span><span class="p">,</span>
            <span class="s1">&#39;to_field&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="o">.</span><span class="n">to_field</span><span class="p">,</span>
            <span class="s1">&#39;cl&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="p">,</span>
            <span class="s1">&#39;media&#39;</span><span class="p">:</span> <span class="n">media</span><span class="p">,</span>
            <span class="s1">&#39;has_add_permission&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">cl</span><span class="o">.</span><span class="n">opts</span><span class="p">,</span>
            <span class="s1">&#39;action_form&#39;</span><span class="p">:</span> <span class="n">action_form</span><span class="p">,</span>
            <span class="s1">&#39;actions_on_top&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">actions_on_top</span><span class="p">,</span>
            <span class="s1">&#39;actions_on_bottom&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">actions_on_bottom</span><span class="p">,</span>
            <span class="s1">&#39;actions_selection_counter&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">actions_selection_counter</span><span class="p">,</span>
            <span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{}),</span>
        <span class="p">}</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">change_list_template</span> <span class="ow">or</span> <span class="p">[</span>
            <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">/change_list.html&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
            <span class="s1">&#39;admin/</span><span class="si">%s</span><span class="s1">/change_list.html&#39;</span> <span class="o">%</span> <span class="n">app_label</span><span class="p">,</span>
            <span class="s1">&#39;admin/change_list.html&#39;</span>
        <span class="p">],</span> <span class="n">context</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.get_deleted_objects"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.get_deleted_objects">[docs]</a>    <span class="k">def</span> <span class="nf">get_deleted_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Hook for customizing the delete process for the delete view and the</span>
<span class="sd">        &quot;delete selected&quot; action.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">get_deleted_objects</span><span class="p">(</span><span class="n">objs</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelAdmin.delete_view"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.delete_view">[docs]</a>    <span class="nd">@csrf_protect_m</span>
    <span class="k">def</span> <span class="nf">delete_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delete_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_delete_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">extra_context</span><span class="p">):</span>
        <span class="s2">&quot;The &#39;delete&#39; admin view for this model.&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span>

        <span class="n">to_field</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">TO_FIELD_VAR</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">to_field</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_field_allowed</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">to_field</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">DisallowedModelAdminToField</span><span class="p">(</span><span class="s2">&quot;The field </span><span class="si">%s</span><span class="s2"> cannot be referenced.&quot;</span> <span class="o">%</span> <span class="n">to_field</span><span class="p">)</span>

        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">unquote</span><span class="p">(</span><span class="n">object_id</span><span class="p">),</span> <span class="n">to_field</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">PermissionDenied</span>

        <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_obj_does_not_exist_redirect</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">opts</span><span class="p">,</span> <span class="n">object_id</span><span class="p">)</span>

        <span class="c1"># Populate deleted_objects, a data structure of all related objects that</span>
        <span class="c1"># will also be deleted.</span>
        <span class="n">deleted_objects</span><span class="p">,</span> <span class="n">model_count</span><span class="p">,</span> <span class="n">perms_needed</span><span class="p">,</span> <span class="n">protected</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_deleted_objects</span><span class="p">([</span><span class="n">obj</span><span class="p">],</span> <span class="n">request</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">protected</span><span class="p">:</span>  <span class="c1"># The user has confirmed the deletion.</span>
            <span class="k">if</span> <span class="n">perms_needed</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">PermissionDenied</span>
            <span class="n">obj_display</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
            <span class="n">attr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">to_field</span><span class="p">)</span> <span class="k">if</span> <span class="n">to_field</span> <span class="k">else</span> <span class="n">opts</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span>
            <span class="n">obj_id</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">serializable_value</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log_deletion</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">obj_display</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_delete</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj_display</span><span class="p">,</span> <span class="n">obj_id</span><span class="p">)</span>

        <span class="n">object_name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">perms_needed</span> <span class="ow">or</span> <span class="n">protected</span><span class="p">:</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Cannot delete </span><span class="si">%(name)s</span><span class="s2">&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">object_name</span><span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Are you sure?&quot;</span><span class="p">)</span>

        <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
            <span class="s1">&#39;object_name&#39;</span><span class="p">:</span> <span class="n">object_name</span><span class="p">,</span>
            <span class="s1">&#39;object&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="p">,</span>
            <span class="s1">&#39;deleted_objects&#39;</span><span class="p">:</span> <span class="n">deleted_objects</span><span class="p">,</span>
            <span class="s1">&#39;model_count&#39;</span><span class="p">:</span> <span class="nb">dict</span><span class="p">(</span><span class="n">model_count</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span>
            <span class="s1">&#39;perms_lacking&#39;</span><span class="p">:</span> <span class="n">perms_needed</span><span class="p">,</span>
            <span class="s1">&#39;protected&#39;</span><span class="p">:</span> <span class="n">protected</span><span class="p">,</span>
            <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">,</span>
            <span class="s1">&#39;app_label&#39;</span><span class="p">:</span> <span class="n">app_label</span><span class="p">,</span>
            <span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;is_popup&#39;</span><span class="p">:</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span> <span class="ow">or</span> <span class="n">IS_POPUP_VAR</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">,</span>
            <span class="s1">&#39;to_field&#39;</span><span class="p">:</span> <span class="n">to_field</span><span class="p">,</span>
            <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{}),</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_delete_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>

<div class="viewcode-block" id="ModelAdmin.history_view"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.history_view">[docs]</a>    <span class="k">def</span> <span class="nf">history_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="s2">&quot;The &#39;history&#39; admin view for this model.&quot;</span>
        <span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="k">import</span> <span class="n">LogEntry</span>
        <span class="c1"># First check if the user can see this history.</span>
        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">unquote</span><span class="p">(</span><span class="n">object_id</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_obj_does_not_exist_redirect</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="n">object_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">PermissionDenied</span>

        <span class="c1"># Then get the history for this object.</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="n">app_label</span> <span class="o">=</span> <span class="n">opts</span><span class="o">.</span><span class="n">app_label</span>
        <span class="n">action_list</span> <span class="o">=</span> <span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
            <span class="n">object_id</span><span class="o">=</span><span class="n">unquote</span><span class="p">(</span><span class="n">object_id</span><span class="p">),</span>
            <span class="n">content_type</span><span class="o">=</span><span class="n">get_content_type_for_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="p">)</span><span class="o">.</span><span class="n">select_related</span><span class="p">()</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s1">&#39;action_time&#39;</span><span class="p">)</span>

        <span class="n">context</span> <span class="o">=</span> <span class="p">{</span>
            <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Change history: </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">obj</span><span class="p">,</span>
            <span class="s1">&#39;action_list&#39;</span><span class="p">:</span> <span class="n">action_list</span><span class="p">,</span>
            <span class="s1">&#39;module_name&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">capfirst</span><span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">)),</span>
            <span class="s1">&#39;object&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="p">,</span>
            <span class="s1">&#39;opts&#39;</span><span class="p">:</span> <span class="n">opts</span><span class="p">,</span>
            <span class="s1">&#39;preserved_filters&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_preserved_filters</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="o">**</span><span class="p">(</span><span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{}),</span>
        <span class="p">}</span>

        <span class="n">request</span><span class="o">.</span><span class="n">current_app</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">name</span>

        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_history_template</span> <span class="ow">or</span> <span class="p">[</span>
            <span class="s2">&quot;admin/</span><span class="si">%s</span><span class="s2">/</span><span class="si">%s</span><span class="s2">/object_history.html&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">opts</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
            <span class="s2">&quot;admin/</span><span class="si">%s</span><span class="s2">/object_history.html&quot;</span> <span class="o">%</span> <span class="n">app_label</span><span class="p">,</span>
            <span class="s2">&quot;admin/object_history.html&quot;</span>
        <span class="p">],</span> <span class="n">context</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_create_formsets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="s2">&quot;Helper function to generate formsets for add/change_view.&quot;</span>
        <span class="n">formsets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">inline_instances</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">prefixes</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">get_formsets_args</span> <span class="o">=</span> <span class="p">[</span><span class="n">request</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">change</span><span class="p">:</span>
            <span class="n">get_formsets_args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">FormSet</span><span class="p">,</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_formsets_with_inlines</span><span class="p">(</span><span class="o">*</span><span class="n">get_formsets_args</span><span class="p">):</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="n">FormSet</span><span class="o">.</span><span class="n">get_default_prefix</span><span class="p">()</span>
            <span class="n">prefixes</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span> <span class="o">=</span> <span class="n">prefixes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">prefixes</span><span class="p">[</span><span class="n">prefix</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">1</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">prefix</span><span class="p">:</span>
                <span class="n">prefix</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">prefixes</span><span class="p">[</span><span class="n">prefix</span><span class="p">])</span>
            <span class="n">formset_params</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s1">&#39;instance&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="p">,</span>
                <span class="s1">&#39;prefix&#39;</span><span class="p">:</span> <span class="n">prefix</span><span class="p">,</span>
                <span class="s1">&#39;queryset&#39;</span><span class="p">:</span> <span class="n">inline</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;POST&#39;</span><span class="p">:</span>
                <span class="n">formset_params</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
                    <span class="s1">&#39;data&#39;</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span>
                    <span class="s1">&#39;files&#39;</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">,</span>
                    <span class="s1">&#39;save_as_new&#39;</span><span class="p">:</span> <span class="s1">&#39;_saveasnew&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span>
                <span class="p">})</span>
            <span class="n">formset</span> <span class="o">=</span> <span class="n">FormSet</span><span class="p">(</span><span class="o">**</span><span class="n">formset_params</span><span class="p">)</span>

            <span class="k">def</span> <span class="nf">user_deleted_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
                <span class="sd">&quot;&quot;&quot;Return whether or not the user deleted the form.&quot;&quot;&quot;</span>
                <span class="k">return</span> <span class="p">(</span>
                    <span class="n">inline</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">-</span><span class="si">{}</span><span class="s1">-DELETE&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">formset</span><span class="o">.</span><span class="n">prefix</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">POST</span>
                <span class="p">)</span>

            <span class="c1"># Bypass validation of each view-only inline form (since the form&#39;s</span>
            <span class="c1"># data won&#39;t be in request.POST), unless the form was deleted.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">inline</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span> <span class="k">if</span> <span class="n">change</span> <span class="k">else</span> <span class="kc">None</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">form</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">formset</span><span class="o">.</span><span class="n">initial_forms</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">user_deleted_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
                        <span class="k">continue</span>
                    <span class="n">form</span><span class="o">.</span><span class="n">_errors</span> <span class="o">=</span> <span class="p">{}</span>
                    <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">initial</span>
            <span class="n">formsets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">formset</span><span class="p">)</span>
            <span class="n">inline_instances</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inline</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">inline_instances</span></div>


<span class="k">class</span> <span class="nc">InlineModelAdmin</span><span class="p">(</span><span class="n">BaseModelAdmin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Options for inline editing of ``model`` instances.</span>

<span class="sd">    Provide ``fk_name`` to specify the attribute name of the ``ForeignKey``</span>
<span class="sd">    from ``model`` to its parent. This is required if ``model`` has more than</span>
<span class="sd">    one ``ForeignKey`` to its parent.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">fk_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">formset</span> <span class="o">=</span> <span class="n">BaseInlineFormSet</span>
    <span class="n">extra</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="n">min_num</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">max_num</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">template</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">verbose_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">can_delete</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">show_change_link</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">checks_class</span> <span class="o">=</span> <span class="n">InlineModelAdminChecks</span>
    <span class="n">classes</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_model</span><span class="p">,</span> <span class="n">admin_site</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span> <span class="o">=</span> <span class="n">admin_site</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent_model</span> <span class="o">=</span> <span class="n">parent_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_registered_model</span> <span class="o">=</span> <span class="n">admin_site</span><span class="o">.</span><span class="n">is_registered</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">extra</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span> <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span> <span class="k">else</span> <span class="s1">&#39;.min&#39;</span>
        <span class="n">js</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;vendor/jquery/jquery</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">,</span> <span class="s1">&#39;jquery.init.js&#39;</span><span class="p">,</span>
              <span class="s1">&#39;inlines</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_vertical</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_horizontal</span><span class="p">:</span>
            <span class="n">js</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s1">&#39;SelectBox.js&#39;</span><span class="p">,</span> <span class="s1">&#39;SelectFilter2.js&#39;</span><span class="p">])</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">classes</span> <span class="ow">and</span> <span class="s1">&#39;collapse&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">classes</span><span class="p">:</span>
            <span class="n">js</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;collapse</span><span class="si">%s</span><span class="s1">.js&#39;</span> <span class="o">%</span> <span class="n">extra</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">forms</span><span class="o">.</span><span class="n">Media</span><span class="p">(</span><span class="n">js</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;admin/js/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">url</span> <span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">js</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">_has_add_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="c1"># RemovedInDjango30Warning: obj will be a required argument.</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">get_func_args</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">if</span> <span class="s1">&#39;obj&#39;</span> <span class="ow">in</span> <span class="n">args</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_extra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Hook for customizing the number of extra inline forms.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra</span>

    <span class="k">def</span> <span class="nf">get_min_num</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Hook for customizing the min number of inline forms.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_num</span>

    <span class="k">def</span> <span class="nf">get_max_num</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Hook for customizing the max number of extra inline forms.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_num</span>

    <span class="k">def</span> <span class="nf">get_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a BaseInlineFormSet class for use in admin add/change views.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s1">&#39;fields&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;fields&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">flatten_fieldsets</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_fieldsets</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
        <span class="n">excluded</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_exclude</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">excluded</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="nb">list</span><span class="p">(</span><span class="n">excluded</span><span class="p">)</span>
        <span class="n">exclude</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_readonly_fields</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">excluded</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span><span class="p">:</span>
            <span class="c1"># Take the custom ModelForm&#39;s Meta.exclude into account only if the</span>
            <span class="c1"># InlineModelAdmin doesn&#39;t define its own.</span>
            <span class="n">exclude</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">exclude</span><span class="p">)</span>
        <span class="c1"># If exclude is an empty list we use None, since that&#39;s the actual</span>
        <span class="c1"># default.</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="n">exclude</span> <span class="ow">or</span> <span class="kc">None</span>
        <span class="n">can_delete</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">can_delete</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;form&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">,</span>
            <span class="s1">&#39;formset&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">formset</span><span class="p">,</span>
            <span class="s1">&#39;fk_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">fk_name</span><span class="p">,</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="n">fields</span><span class="p">,</span>
            <span class="s1">&#39;exclude&#39;</span><span class="p">:</span> <span class="n">exclude</span><span class="p">,</span>
            <span class="s1">&#39;formfield_callback&#39;</span><span class="p">:</span> <span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">formfield_for_dbfield</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="n">request</span><span class="p">),</span>
            <span class="s1">&#39;extra&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_extra</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="s1">&#39;min_num&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_min_num</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="s1">&#39;max_num&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_max_num</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span>
            <span class="s1">&#39;can_delete&#39;</span><span class="p">:</span> <span class="n">can_delete</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="n">base_model_form</span> <span class="o">=</span> <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]</span>
        <span class="n">can_change</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">if</span> <span class="n">request</span> <span class="k">else</span> <span class="kc">True</span>
        <span class="n">can_add</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span> <span class="k">if</span> <span class="n">request</span> <span class="k">else</span> <span class="kc">True</span>

        <span class="k">class</span> <span class="nc">DeleteProtectedModelForm</span><span class="p">(</span><span class="n">base_model_form</span><span class="p">):</span>

            <span class="k">def</span> <span class="nf">hand_clean_DELETE</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
                <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">                We don&#39;t validate the &#39;DELETE&#39; field itself because on</span>
<span class="sd">                templates it&#39;s not rendered using the field information, but</span>
<span class="sd">                just using a generic &quot;deletion_field&quot; of the InlineModelAdmin.</span>
<span class="sd">                &quot;&quot;&quot;</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">DELETION_FIELD_NAME</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
                    <span class="n">using</span> <span class="o">=</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                    <span class="n">collector</span> <span class="o">=</span> <span class="n">NestedObjects</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span><span class="p">:</span>
                        <span class="k">return</span>
                    <span class="n">collector</span><span class="o">.</span><span class="n">collect</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">])</span>
                    <span class="k">if</span> <span class="n">collector</span><span class="o">.</span><span class="n">protected</span><span class="p">:</span>
                        <span class="n">objs</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">collector</span><span class="o">.</span><span class="n">protected</span><span class="p">:</span>
                            <span class="n">objs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                                <span class="c1"># Translators: Model verbose name and instance representation,</span>
                                <span class="c1"># suitable to be an item in a list.</span>
                                <span class="n">_</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%(class_name)s</span><span class="s1"> </span><span class="si">%(instance)s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span>
                                    <span class="s1">&#39;class_name&#39;</span><span class="p">:</span> <span class="n">p</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
                                    <span class="s1">&#39;instance&#39;</span><span class="p">:</span> <span class="n">p</span><span class="p">}</span>
                            <span class="p">)</span>
                        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
                            <span class="s1">&#39;class_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">,</span>
                            <span class="s1">&#39;instance&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span>
                            <span class="s1">&#39;related_objects&#39;</span><span class="p">:</span> <span class="n">get_text_list</span><span class="p">(</span><span class="n">objs</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;and&#39;</span><span class="p">)),</span>
                        <span class="p">}</span>
                        <span class="n">msg</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Deleting </span><span class="si">%(class_name)s</span><span class="s2"> </span><span class="si">%(instance)s</span><span class="s2"> would require &quot;</span>
                                <span class="s2">&quot;deleting the following protected related objects: &quot;</span>
                                <span class="s2">&quot;</span><span class="si">%(related_objects)s</span><span class="s2">&quot;</span><span class="p">)</span>
                        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;deleting_protected&#39;</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>

            <span class="k">def</span> <span class="nf">is_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
                <span class="n">result</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">hand_clean_DELETE</span><span class="p">()</span>
                <span class="k">return</span> <span class="n">result</span>

            <span class="k">def</span> <span class="nf">has_changed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
                <span class="c1"># Protect against unauthorized edits.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">can_change</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">can_add</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
                <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">has_changed</span><span class="p">()</span>

        <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">DeleteProtectedModelForm</span>

        <span class="k">if</span> <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">modelform_defines_fields</span><span class="p">(</span><span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]):</span>
            <span class="n">defaults</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">ALL_FIELDS</span>

        <span class="k">return</span> <span class="n">inlineformset_factory</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent_model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="n">defaults</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_form_for_get_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">form</span>

    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_view_or_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
            <span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">queryset</span>

    <span class="k">def</span> <span class="nf">_has_any_perms_for_target_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">perms</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is called only when the ModelAdmin&#39;s model is for an</span>
<span class="sd">        ManyToManyField&#39;s implicit through model (if self.opts.auto_created).</span>
<span class="sd">        Return True if the user has any of the given permissions (&#39;add&#39;,</span>
<span class="sd">        &#39;change&#39;, etc.) for the model that points to the through model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="c1"># Find the target model of an auto-created many-to-many relationship.</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">opts</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span> <span class="ow">and</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent_model</span><span class="p">:</span>
                <span class="n">opts</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span>
                <span class="k">break</span>
        <span class="k">return</span> <span class="nb">any</span><span class="p">(</span>
            <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="n">perm</span><span class="p">,</span> <span class="n">opts</span><span class="p">)))</span>
            <span class="k">for</span> <span class="n">perm</span> <span class="ow">in</span> <span class="n">perms</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_add_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># RemovedInDjango30Warning: obj becomes a mandatory argument.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="c1"># Auto-created intermediate models don&#39;t have their own</span>
            <span class="c1"># permissions. The user needs to have the change permission for the</span>
            <span class="c1"># related model in order to be able to do anything with the</span>
            <span class="c1"># intermediate model.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_any_perms_for_target_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;change&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">has_add_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_change_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="c1"># Same comment as has_add_permission().</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_any_perms_for_target_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;change&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">has_change_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_delete_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="c1"># Same comment as has_add_permission().</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_any_perms_for_target_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;change&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">has_delete_permission</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_view_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
            <span class="c1"># Same comment as has_add_permission(). The &#39;change&#39; permission</span>
            <span class="c1"># also implies the &#39;view&#39; permission.</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_any_perms_for_target_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="s1">&#39;change&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">has_view_permission</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>


<div class="viewcode-block" id="StackedInline"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.StackedInline">[docs]</a><span class="k">class</span> <span class="nc">StackedInline</span><span class="p">(</span><span class="n">InlineModelAdmin</span><span class="p">):</span>
    <span class="n">template</span> <span class="o">=</span> <span class="s1">&#39;admin/edit_inline/stacked.html&#39;</span></div>


<div class="viewcode-block" id="TabularInline"><a class="viewcode-back" href="../../../../ref/contrib/admin/index.html#django.contrib.admin.TabularInline">[docs]</a><span class="k">class</span> <span class="nc">TabularInline</span><span class="p">(</span><span class="n">InlineModelAdmin</span><span class="p">):</span>
    <span class="n">template</span> <span class="o">=</span> <span class="s1">&#39;admin/edit_inline/tabular.html&#39;</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>